package com.galleryviewpager.f_pane;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

import com.galleryviewpager.DensityUtils;
import com.galleryviewpager.R;

public class PanelView extends View {

    private Context mContext;
    private int mArcColor;
    private int mMinCircleColor;
    private int mTikeCount;
    private int mTextSize;
    private String mText;
    private int mScendArcWidth;
    private int mWidth, mHeigth;
    private int mPercent = 50;
    private int mMinCircleRadius;
    private int mTikeWidth;

    public PanelView(Context context) {
        this(context, null);
    }

    public PanelView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public PanelView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        TypedArray typedArray = mContext.obtainStyledAttributes(attrs, R.styleable.PanelView, defStyleAttr, 0);

        mArcColor = typedArray.getColor(R.styleable.PanelView_arcColor, Color.RED);
        mMinCircleColor = typedArray.getColor(R.styleable.PanelView_pointerColor, Color.GREEN);
        mTikeCount = typedArray.getInt(R.styleable.PanelView_tikeCount, 12);

        mTextSize = typedArray.getDimensionPixelSize(R.styleable.PanelView_android_textSize, DensityUtils.sp2px(mContext, 24));
        mText = typedArray.getString(R.styleable.PanelView_android_text);
        mScendArcWidth = 100;

        typedArray.recycle();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);

        if (widthMode == MeasureSpec.EXACTLY) {
            mWidth = widthSize;
        } else {
            mWidth = DensityUtils.dip2px(mContext, 200);
        }

        int heightSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(widthMeasureSpec);
        if (heightMode == MeasureSpec.EXACTLY) {
            mHeigth = heightSize;
        } else {
            mHeigth = DensityUtils.dip2px(mContext, 200);
        }
        setMeasuredDimension(mWidth, mHeigth);


    }

    @Override
    protected void onDraw(Canvas canvas) {
        Paint paint = new Paint();
        int strokeWidth = 3;
        paint.setStrokeWidth(strokeWidth);
        paint.setAntiAlias(true);
        paint.setStyle(Paint.Style.STROKE);
        paint.setColor(mArcColor);
        //最外面的线条
        canvas.drawArc(new RectF(strokeWidth, strokeWidth, mWidth - strokeWidth, mHeigth - strokeWidth), 145, 250, false, paint);

        RectF secondRectF = new RectF(strokeWidth + 50, strokeWidth + 50, mWidth - strokeWidth - 50, mHeigth - strokeWidth - 50);
        int secondRectWidth = mWidth - strokeWidth - 50 - (strokeWidth + 50);
        int secondRectHeight = mHeigth - strokeWidth - 50 - (strokeWidth + 50);
        float percent = mPercent / 100f;

        float fill = 250 * percent;

        float empty = 250 - fill;
        if (percent == 0) {
            paint.setColor(Color.WHITE);
        }
        canvas.drawRect(secondRectF, paint);
        paint.setStrokeWidth(20f);
        canvas.drawArc(secondRectF, 135, 11, false, paint);

        canvas.drawArc(secondRectF, 145, fill, false, paint);

        paint.setColor(Color.GREEN);
        canvas.drawArc(secondRectF, 145 + fill, empty, false, paint);

        if (percent == 1) {
            paint.setColor(mArcColor);
        }
        canvas.drawArc(secondRectF, 144 + fill + empty, 10, false, paint);

        paint.setColor(mMinCircleColor);
        paint.setStrokeWidth(8);
        mMinCircleRadius = 15;
        canvas.drawCircle(mWidth / 2, mHeigth / 2, mMinCircleRadius, paint);

        //绘制刻度
        paint.setColor(mArcColor);
        mTikeWidth = 20;
        paint.setStrokeWidth(3);

        canvas.drawLine(mWidth / 2, 0, mWidth / 2, mTikeWidth, paint);

        float rAngle = 250f / mTikeCount;

        for (int i = 0; i < mTikeCount / 2; i++) {
            canvas.rotate(rAngle, mWidth / 2, mHeigth / 2);
            canvas.drawLine(mWidth / 2, 0, mWidth / 2, mTikeWidth, paint);
        }

        canvas.rotate(-rAngle * mTikeCount / 2, mWidth / 2, mHeigth / 2);

        for (int i = 0; i < mTikeCount / 2; i++) {
            canvas.rotate(-rAngle, mWidth / 2, mHeigth / 2);
            canvas.drawLine(mWidth / 2, 0, mWidth / 2, mTikeWidth, paint);
        }
        canvas.rotate(rAngle * mTikeCount / 2, mWidth / 2, mHeigth / 2);


        paint.setColor(mMinCircleColor);
        paint.setStrokeWidth(4);
        canvas.rotate(250 * percent - 250 / 2, mWidth / 2, mHeigth / 2);

        canvas.drawLine(mWidth / 2, (mHeigth / 2 - secondRectHeight / 2) + mScendArcWidth / 2 + 2, mWidth / 2, mHeigth / 2 - mMinCircleRadius, paint);

        canvas.rotate(-(250 * percent - 250 / 2), mWidth / 2, mHeigth / 2);
    }
}
